package sharing.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;

/**
 * Servlet implementation class CreateAccount
 */
@WebServlet("/CreateAccount")
public class CreateAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateAccount() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String s = request.getQueryString();
		PrintWriter out = response.getWriter();
		out.print(s);
		out.print("aze");
		
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("PDW_JPA");
		EntityManager em = emf.createEntityManager();

		EntityTransaction t = em.getTransaction();

		Client client = new Client();

		client.setLogin("azeezaezaeza");
		client.setPassword("azzzzzzaaaa");

		t.begin();
		em.persist(client);
		t.commit();
		
//		SharingPK sharingPK = new SharingPK();
//		sharingPK.setIdClient(client.getId());
//		Sharing sharing = new Sharing();
//		sharing.setId(sharingPK);
//		
//		t.begin();
//		em.persist(sharing);
//		t.commit();
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPut(req, resp);
		InputStream input = req.getInputStream();
//
//		BufferedReader buf = new BufferedReader(new InputStreamReader(input));
//		String sLine;
//
//		StringBuilder o_oSb = new StringBuilder();
//		while ((sLine = buf.readLine()) != null) {
//			o_oSb.append(sLine);
//			o_oSb.append("\n");
//		}

//		PrintWriter out = resp.getWriter();
//		out.print(o_oSb);
		
		BufferedReader buf = new BufferedReader(new InputStreamReader(input));
		byte [] b = null;
		try {
			b = Base64.decode(buf);
			
		} catch (Base64DecodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		String s = new String(b);
		PrintWriter out = resp.getWriter();
		out.print(s);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub


		
	}

}
